import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

const vcot = 'vcode_obtaining_time';
const code = 'vcode';
const vt = 1 * 60 * 1000;
const store = new Vuex.Store({
  state: {
    vCode: '',
  },
  getters: {
    vCode(state) {
      const info = JSON.parse(localStorage.getItem(code));
      if (info) {
        const time = localStorage.getItem(vcot);
        if (time) {
          const ftime = vt + time;
          const ntime = Date.parse(new Date());
          if (ftime > ntime) {
            console.log('验证码未过期');
            state.vCode = info;
            return state.vCode;
          }
        }
        console.log('验证码已过期');
        localStorage.setItem(code, '');
      }
      return state.vCode;
    },
  },
  mutations: {
    getCode(state, payload) {
      state.vCode = payload;
      localStorage.setItem(code, JSON.stringify(payload));
      const startTime = Date.parse(new Date());
      localStorage.setItem(vcot, startTime);
    },
  },
  actions: {
    getCode(context, payload) {
      context.commit('getCode', payload);
    },

  },
});
export default store;
